package com.softeem.music_sys.controller;

import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Comment;
import com.softeem.music_sys.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论控制器
 */
@Tag(name = "评论接口", description = "评论相关操作:发布、查询、删除、点赞等")
@RestController
@RequestMapping("/comment")
public class CommentController {
    
    @Autowired
    private CommentService commentService;

    @Operation(summary = "发布评论", description = "发布新评论")
    @PostMapping
    public Result add(@RequestBody Comment comment) {
        return commentService.addComment(comment);
    }

    @Operation(summary = "删除评论", description = "根据ID删除评论")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return commentService.deleteComment(id);
    }

    @Operation(summary = "获取评论", description = "根据ID获取评论详情")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return commentService.getCommentById(id);
    }

    @Operation(summary = "获取对象的评论", description = "获取指定对象的所有评论")
    @Parameters({
            @Parameter(name = "type", description = "评论对象类型"),
            @Parameter(name = "targetId", description = "评论对象ID"),
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/target")
    public Result getByTarget(@RequestParam Object type,
                             @RequestParam Integer targetId,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit) {
        return commentService.getCommentsByTargetController(type, targetId, page, limit);
    }

    @Operation(summary = "获取用户的评论", description = "获取指定用户的所有评论")
    @GetMapping("/user/{userId}")
    public Result getByUser(@PathVariable Integer userId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        return commentService.getCommentsByUserController(userId, page, limit);
    }

    @Operation(summary = "点赞评论", description = "给评论点赞，点赞数+1")
    @PutMapping("/like/{id}")
    public Result like(@PathVariable Integer id) {
        return commentService.likeCommentController(id);
    }

    @Operation(summary = "获取热门评论", description = "根据点赞数获取热门评论")
    @GetMapping("/hot")
    public Result getHotComments(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        return commentService.getHotCommentsController(page, limit);
    }

    @Operation(summary = "获取最新评论", description = "根据时间获取最新评论")
    @GetMapping("/new")
    public Result getNewComments(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        return commentService.getNewCommentsController(page, limit);
    }

    @Operation(summary = "获取所有评论", description = "管理员获取所有评论（包括已删除）")
    @GetMapping("/all")
    public Result getAllComments(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        return commentService.getAllCommentsController(page, limit);
    }
}